package com.fingard.dsp.bank.directbank.abc01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.net.ITcpClient;
import com.fingard.net.TcpPortClient;
import com.fingard.net.TcpReceiver;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextWriter;

import java.util.Date;

/**
 * 中国农业银行银企直联专线版(ABC01)基础方法
 */
public class ABC01Base extends DirectBase implements ITcpClient {
    /**
     * 获取交互字符集
     *
     * @return
     */
    protected String getCharset() {
        return getBankConfig().getCharset("GBK");
    }

    /**
     * 写通用公共报文头
     *
     * @param xmlWriter
     * @param tmpBankHeader
     * @throws Exception
     */
    public void writeCommonHead(XmlTextWriter xmlWriter, ABC01Header tmpBankHeader) throws Exception {
        if (StringHelper.isNullOrEmpty(tmpBankHeader.reqSeqID)) {
            throw new Exception("流水号未赋值");
        }
        if (StringHelper.isNullOrEmpty(tmpBankHeader.transCode)) {
            throw new Exception("交易代码为空");
        }
        xmlWriter.writeStartElement("ap");
        xmlWriter.writeElementString("TransCode", tmpBankHeader.transCode);
        xmlWriter.writeElementString("ChannelType", "0");
        xmlWriter.writeElementString("CorpNo", bankFront.org);
        xmlWriter.writeElementString("OpNo", bankFront.operator);
        xmlWriter.writeElementClosed("AuthNo");
        xmlWriter.writeElementString("ReqSeqNo", tmpBankHeader.reqSeqID);
        Date tmpDateNow = new Date();
        xmlWriter.writeElementString("ReqDate", Format.Date8Format.format(tmpDateNow));
        xmlWriter.writeElementString("ReqTime", Format.Time6Format.format(tmpDateNow));
        xmlWriter.writeElementClosed("Sign");
    }

    /**
     * 发送数据到银行
     *
     * @param tmpReqData
     * @return
     * @throws Exception
     */
    protected String[] sendToBank(String tmpReqData) throws Exception {
        String[] tmpStrRet = new String[]{"", ""};

        byte[] tmpBytesBody = tmpReqData.getBytes(getCharset());
        String tmpHead = "0" + String.format("%-6d", tmpBytesBody.length);
        byte[] tmpBytesHead = tmpHead.getBytes(getCharset());

        byte[] tmpBytesSend = new byte[tmpBytesHead.length + tmpBytesBody.length];
        System.arraycopy(tmpBytesHead, 0, tmpBytesSend, 0, tmpBytesHead.length);
        System.arraycopy(tmpBytesBody, 0, tmpBytesSend, tmpBytesHead.length, tmpBytesBody.length);
        WriteBankLogStep2(tmpHead + tmpReqData);

        TcpPortClient tmpClient = new TcpPortClient(bankFront.serverIP, Integer.parseInt(bankFront.serverPort), this);
        WriteBankUrlLogLn(bankFront.serverIP + ":" + bankFront.serverPort);

        TcpReceiver tmpReceiver = tmpClient.sendMessage(tmpBytesSend);
        tmpStrRet[0] = tmpReceiver.connState;
        if (tmpReceiver.recBytes != null) {
            tmpStrRet[1] = new String(tmpReceiver.recBytes, 0, tmpReceiver.readedLength, getCharset());
        }
        WriteBankLogStep3(tmpStrRet[1]);

        if (tmpStrRet[1].contains("<")) {
            tmpStrRet[1] = tmpStrRet[1].substring(tmpStrRet[1].indexOf("<"));
        }
        if (!tmpReceiver.isCompleted) {
            WriteBankLogLn(tmpReceiver.getConnMsg());
        }

        return tmpStrRet;
    }

    /**
     * 农行账号拼接
     *
     * @param p_actNum
     * @return
     */
    public String getAbcAccNo(String p_actNum) {
        if ((p_actNum.indexOf('-') > 0 && p_actNum.length() == 18) || (p_actNum.length() == 17)) {
            return p_actNum.substring(p_actNum.length() - 15);
        }
        return p_actNum;
    }

    /**
     * 农行区域赋值
     *
     * @param tmpActNum
     * @param tmpActArea
     * @return
     */
    public String getAbcAccProv(String pActNum, String pActArea) {
        if ((pActNum.indexOf('-') > 0 && pActNum.length() == 18) || (pActNum.length() == 17)) {
            return pActNum.substring(0, 2);
        }
        return pActArea;
    }

    @Override
    public void getContentLength(TcpReceiver p_receiver) throws Exception {
        if (p_receiver.contentLength == -1) {
            if (p_receiver.readedLength >= 7) {
                String tmpHead = new String(p_receiver.recBytes, 1, 6, getCharset());
                p_receiver.contentLength = Integer.parseInt(tmpHead.trim());
                p_receiver.contentStartIndex = 7;
            }
        }
    }
}